extends CharacterBody2D
class_name player

@onready var anim_tree: AnimationTree = $anim_tree
@onready var anim_tree_prop = $anim_tree.get("parameters/playback")
@onready var sprite:Sprite2D = $sprite
#初始化坐标
var inital_position: Vector2 = Vector2.ZERO
#
var previous_positon: Vector2 = Vector2.ZERO
#初始化输入方向
var input_direction: Vector2 = Vector2.ZERO


var is_moving: bool = false

var poke_percent: float = 0.0

var can_move: bool = true
const TILE_SIZE: int = 16
var percent: float = 0.0
var can_transition: bool = true
var transitioning: bool = false

var walk_speed : float = 6.0
var can_animated_transition: bool = false
var entered: Area2D
var new_room: int
var next_position: Vector2 = Vector2.ZERO

func _ready():
	inital_position = position
	previous_positon = inital_position
	anim_tree_prop.travel("walk")
	
func _physics_process(delta):
	pass
	
	#print(Input.get_axis("move_left","move_right"))


func player_input():
	if input_direction.y == 0: input_direction.x = Input.get_axis("move_left","move_right")
	if input_direction.x == 0: input_direction.y = Input.get_axis("move_up", "move_down") 
	var moving_down: bool = !(sprite.frame == 2 or sprite.frame == 0 or sprite.frame == 1 or sprite.frame == 12 or sprite.frame == 13 or sprite.frame == 14)
	var moving_up: bool = !(sprite.frame == 7 or sprite.frame == 8 or sprite.frame == 6 or sprite.frame == 18 or sprite.frame == 19 or sprite.frame == 20)
	var moving_left: bool = !(sprite.frame == 9 or sprite.frame == 10 or sprite.frame == 11  or sprite.frame == 21 or sprite.frame == 22 or sprite.frame == 23)
	var moving_right: bool = !(sprite.frame == 4 or sprite.frame == 5 or sprite.frame == 3 or sprite.frame == 15 or sprite.frame == 16 or sprite.frame == 17)
	
	if input_direction == Vector2(0, 1) and moving_down: 
		anim_tree.active = false
		sprite.frame = 1
		anim_tree_prop.travel("idle")
		
		await get_tree().create_timer(.1).timeout

		anim_tree.active = true
		return
	elif input_direction == Vector2(0, -1) and moving_up:
		anim_tree.active = false
		sprite.frame = 7
		
		await get_tree().create_timer(.1).timeout
	
		anim_tree.active = true
		return
	elif input_direction == Vector2(-1, 0) and moving_left: 
		anim_tree.active = false
		sprite.frame = 11
		
		await get_tree().create_timer(.1).timeout
	
		anim_tree.active = true
		return
	elif input_direction == Vector2(1, 0) and moving_right:
		anim_tree.active = false
		sprite.frame = 4
		anim_tree_prop.travel("idle")
		
		await get_tree().create_timer(.1).timeout
		
		anim_tree.active = true
		return
		
func move(delta: float) -> void:
	
	
	percent += walk_speed * delta
	
	if percent >= 1.0:
		position = inital_position + (TILE_SIZE * input_direction)
		percent = 0.0
		is_moving = false
		
		
	else:
		position = inital_position + (TILE_SIZE * input_direction * percent)
		can_transition = true

func transition() -> void:
	
	process_mode = Node.PROCESS_MODE_DISABLED
	#global.emit_signal("transition", new_room, next_position, 0)
	
	can_transition = true
	transitioning = false
	#touched = false
	
func animated_transition() -> void:
	
	anim_tree.process_mode = Node.PROCESS_MODE_DISABLED
	can_move = false
	
	entered.player_animation()
	
	await get_tree().create_timer(0.3).timeout
	
	anim_tree.process_mode = Node.PROCESS_MODE_INHERIT
	
	await get_tree().create_tween().tween_property(self, "position", position + Vector2(0,-16), 0.25).finished
	#global.transition.emit(new_room, animated_transition_position, 1)
	anim_tree.process_mode = Node.PROCESS_MODE_DISABLED
	can_animated_transition = false


